package com.gaofeng.customviewdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

public class LabelTestView extends AppCompatTextView {
    Path mPath = new Path();
    Paint mPaint = new Paint();

    public LabelTestView(@NonNull Context context) {
        super(context);
        init();
    }

    public LabelTestView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LabelTestView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(2);
        mPaint.setTextSize(30f);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();
        mPath.moveTo(0f, getHeight() >> 1);
        mPath.lineTo(getWidth() >> 1, 0f);
        mPath.close();
//        canvas.drawPath(mPath, mPaint);
        if (getText() != null && getText().length() > 0) {
            canvas.drawTextOnPath(getText().toString(), mPath, 20f, 10f, mPaint);
        }
    }
}
